package com.punch.course.controller;

import com.punch.common.dto.course.ClassRoomDTO;
import com.punch.common.model.Result;
import com.punch.common.page.PageInfo;
import com.punch.common.vo.course.ClassRoomPageVO;
import com.punch.common.vo.course.ClassRoomVO;
import com.punch.common.vo.course.SimpleClassRoomVO;
import com.punch.course.exception.CourseExceptionEnum;
import com.punch.course.query.ClassRoomQueryInfo;
import com.punch.course.query.SelectClassRoomQueryInfo;
import com.punch.course.service.ClassRoomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


/**
 * 教室管理
 *
 * @author xiaol
 * @version 1.0.0
 * @since 2020/7/21
 */
@RestController
@Api(value = "ClassRoomController", tags = "教室管理Controller")
public class ClassRoomController {

    @Resource
    private ClassRoomService classRoomService;

    /**
     * 条件分页查询教室列表
     *
     * @return IPage<ClassRoomPageVO>
     * @author xiaol
     * @since 2020/7/14 10:51
     */
    @GetMapping(value = "/class-rooms", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "条件查询教室列表", notes = "条件查询教室列表", produces = "application/json")
    public Result<PageInfo<ClassRoomPageVO>> listClassRoom(@ModelAttribute ClassRoomQueryInfo queryInfo) {
        return Result.succeed(classRoomService.listClassRoom(queryInfo));
    }

    /**
     * 查询教室详情
     *
     * @param classRoomId 教室记录id
     * @return ClassRoomForm
     * @author xiaol
     * @since 2020/7/14 10:51
     */
    @GetMapping(value = "/class-room/{classRoomId}", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询教室列表详情", notes = "查询教室列表详情", produces = "application/json")
    public Result<ClassRoomVO> getClassRoomById(@PathVariable Long classRoomId) {
        return Result.succeed(classRoomService.getClassRoomById(classRoomId));
    }

    /**
     * 保存教室信息
     *
     * @param classRoomDTO 教室信息表单
     * @return classRoomId
     * @author xiaol
     * @since 2020/7/21
     */

    @PostMapping(value = "/class-room", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "保存教室信息", notes = "保存教室信息", produces = "application/json")
    public Result<Long> saveClassRoom(@Valid @RequestBody ClassRoomDTO classRoomDTO) {
        Long classRoomId = classRoomService.saveClassRoom(classRoomDTO);
        return Result.succeed(classRoomId);
    }

    /**
     * 修改教室信息
     *
     * @param classRoomDTO 教室信息表单
     * @return classRoomId
     * @author xiaol
     * @since 2020/7/21
     */

    @PutMapping(value = "/class-room", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "修改教室信息", notes = "修改教室信息", produces = "application/json")
    public Result updateClassRoom(@Valid @RequestBody ClassRoomDTO classRoomDTO) {
        Long classRoomId = classRoomService.updateClassRoom(classRoomDTO);
        return Result.succeed(classRoomId, CourseExceptionEnum.CLASSROOM_EDIT_SUCCESS.getMsg());
    }

    /**
     * 删除教室信息
     *
     * @param classRoomId 教室id
     * @return 删除教室提示信息
     * @author xiaol
     * @since 2020/7/21
     */
    @DeleteMapping(value = "/class-room/{classRoomId}", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "删除教室信息", notes = "删除教室信息", produces = "application/json")
    public Result<String> deleteClassRoomById(@PathVariable Long classRoomId) {
        try {
            classRoomService.deleteClassRoomById(classRoomId);
        } catch (Exception e) {
            return Result.failed(CourseExceptionEnum.CLASSROOM_DEL_FAIL.getMsg());
        }

        return Result.succeed(CourseExceptionEnum.CLASSROOM_DEL_SUCCESS.getMsg());
    }

    /**
     * 教室下拉列表
     *
     * @return List<SimpleClassRoomVO>
     * @author xiaol
     * @since 2020/7/23
     */
    @GetMapping(value = "/class-rooms/simple", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "教室下拉列表", notes = "教室下拉列表", produces = "application/json")
    public Result<List<SimpleClassRoomVO>> listClassRoomSimple(@RequestParam(name = "classRoomName", required = false) String classRoomName,
                                                               @RequestParam(name = "classRoomId", required = false) Long classRoomId) {
        return Result.succeed(classRoomService.listClassRoomSimple(classRoomName,classRoomId));
    }

    /**
     * 教室启用/停用
     */
    @PutMapping("/class-room/enable/{classRoomId}")
    @ApiOperation(value = "教室启用/停用")
    public Result enableClassRoom(@PathVariable Long classRoomId) {
        classRoomService.enableClassRoom(classRoomId);
        return Result.succeed("修改成功");
    }

    /**
     * 教室导入
     *
     * @param file 文件
     * @return string
     */
    @PostMapping(value = "/class-room/import", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "教室导入", notes = "教室导入", produces = "application/json")
    public Result importClassRoom(@RequestParam("file") MultipartFile file) {
        classRoomService.importClassRoom(file);
        return Result.succeed("导入成功");
    }
}
